<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>放大镜测试</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .glass1 {
            width: 600px;
            height: 600px;
            margin: 50px auto;
            position: relative;
            border: 1px solid red;
        }

        .glass1 .g-left {
            position: absolute;
            top: 0;
            left: 0;
            width: 200px;
            height: 200px;
            border: 1px solid rgb(32, 32, 31);
        }

        .glass1 .g-left img {
            width: 200px;
            height: 200px;
        }

        .glass1 .g-left .mask {
            position: absolute;
            top: 0;
            left: 0;
            width: 100px;
            height: 100px;
            background-color: rgba(255, 127, 80, .4);
            cursor: move;
            display: none;
        }

        .glass1 .g-right {
            width: 200px;
            height: 200px;
            position: absolute;
            left: 230px;
            top: 0;
            overflow: hidden;
            display: none;
        }

        .wrap .g-right img {
            width: 400px;
            height: 400px;
            position: absolute;
        }
    </style>
</head>

<body>
    <div class="glass1">
        <div class="g-left">
            <img src="./image/show_1.jpg" alt="">
            <div class="mask"></div>
        </div>
        <div class="g-right">
            <img src="./image/show_1.jpg" alt="">
        </div>
    </div>

    <script>
        const showBox = document.querySelector('.g-left')
        const mask = document.querySelector('.mask')
        const glassBox = document.querySelector('.g-right')
        const bigImg = document.querySelector('.g-right>img')

        showBox.onmouseover = function () {
            mask.style.display = 'block'
            glassBox.style.display = 'block'
        }
        showBox.onmouseout = function () {
            console.log('mouseout');
            mask.style.display = 'none'
            glassBox.style.display = 'none'
        }

        showBox.onmousemove = function (e) {
            e = e || window.event
            console.log('showBox.offsetParent.offsetLeft ', showBox.offsetParent.offsetLeft);
            console.log('showBox.offsetLeft ', showBox.offsetLeft);
            let x = e.clientX - showBox.offsetParent.offsetLeft - mask.offsetWidth / 2
            let y = e.clientY - showBox.offsetParent.offsetTop - mask.offsetHeight / 2

            //边界值判断
            if (x <= 0) {
                x = 0
            } else if (x >= showBox.offsetWidth - mask.offsetWidth) {
                x = showBox.offsetWidth - mask.offsetWidth
            }
            if (y <= 0) {
                y = 0
            } else if (y >= showBox.offsetHeight - mask.offsetHeight) {
                y = showBox.offsetHeight - mask.offsetHeight
            }

            mask.style.left = x + 'px'
            mask.style.top = y + 'px'

        }
    </script>
</body>

</html>